package com.uzai.console.service.cdkeys.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.config.UzaiConsoleSysConfig;
import com.uzai.console.dto.alipaydetail.AlipayDetailCreateDto;
import com.uzai.console.entity.AlipayDetail;
import com.uzai.console.enums.AlipayStatusEnum;
import com.uzai.console.enums.AlipayTypeEnum;
import com.uzai.console.mapper.AlipayDetailMapper;
import com.uzai.console.service.alipaydetail.AlipayDetailService;
import com.uzai.console.service.cdkeys.AlipayForCdkeysService;
import com.uzai.console.vo.alipaydetail.AlipayDetailCreateVo;
import com.uzai.console.vo.alipaydetail.AlipayDetailInfoVo;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.checkerframework.checker.units.qual.A;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liuqi
 * @date 2021年08月23日 20:25
 */
@Service
public class AlipayForCdkeysServiceImpl implements AlipayForCdkeysService {

    Logger logger = LoggerFactory.getLogger(AlipayForCdkeysServiceImpl.class);

    @Autowired
    private AlipayDetailService alipayDetailService;

    @Autowired
    private AlipayDetailMapper alipayDetailMapper;

    @Autowired
    private UzaiConsoleSysConfig uzaiConsoleSysConfig;

    @Override
    public String createAlipayDetail(AlipayDetailCreateDto alipayDetailCreateDto) {
        //完成数据进行对应

        int now = DateUtil.getNowTime();

        //添加支付宝账单
        AlipayDetail alipayDetail = new AlipayDetail();
        alipayDetail.setId(IdWorker.getId());
        alipayDetail.setMerId(0L);
        alipayDetail.setTradeno(alipayDetailCreateDto.getOutTradeNo());
        alipayDetail.setType(AlipayTypeEnum.CDKEY_AGENT_RECHARGE.getId()); //快站域名购买
        //业务信息暂不设置
        //        alipayDetail.setBusinessInfo(Tools.getStrEmpty(JSONObject.toJSONString(kuaiZhanDomianBuyBusinessInfo)));
        alipayDetail.setAmount(alipayDetailCreateDto.getTotalAmount());
        alipayDetail.setStatus(AlipayStatusEnum.WAIT_BUYER_PAY.getId()); //等待付款
        alipayDetail.setCreateTime(now);
        alipayDetail.setUpdateTime(now);

        //请求支付宝申请订单

        String alipayUrl = alipayDetailService.createAlipayDetail(alipayDetailCreateDto);
        if (StringUtils.isNotBlank(alipayUrl)) {
            alipayDetail.setAlipayUrl(alipayUrl);
            //插入支付宝账单
            alipayDetailMapper.insertSelective(alipayDetail);
            //返回支付页面
            String url = uzaiConsoleSysConfig.getAlipayPaypageUrl() + "?id=" + alipayDetail.getId();
            return url;
        } else {
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "调用支付宝失败");
        }
    }


    @Override
    public AlipayDetailInfoVo alipayTradeQuery(String outTradeNo) {
        alipayDetailService.alipayTradeQuery(outTradeNo);

        AlipayDetail alipayDetail = alipayDetailMapper.selectByTradeno(outTradeNo);

        if (ObjectUtils.allNotNull(alipayDetail)) {
            return JSONObject.parseObject(JSONObject.toJSONString(alipayDetail), AlipayDetailInfoVo.class);
        }
        return null;
    }
}
